(function($){
    $(function(){
        $('#testPromiseBasic').on('click',testPromiseBasice);
        $('#testPromiseInterval').on('click',testPromiseInterval);





    });

    /**
     * promise 基本能力测试
     * 可以挂接多个done
     * @param config
     * @returns {*}
     */
    function testPromiseBasice(){
        var getPicture = function(config){
            var dtd = $.Deferred();
            var success = function(event){
                console.log('work finished');
                dtd.resolve(event);
            };
            var error = function(event){
                console.log('work error');
                debugger;
                dtd.reject(event);
            };
            navigator.geolocation.getCurrentPosition(success,error,config);
            return dtd.promise();
        };
        var promise = getPicture([]);

        promise.done(function(event){
            debugger;
            console.log('done');
        }).fail(function(event){
                debugger;
                console.log('fail');
            }).done(function(event){
                debugger;
                console.log('another done')
            }).fail(function(event){
                debugger;
                console.log('another fail');
            });
    }


    /**
     * promise 能力测试
     * 可以多次触发done
     * @param config
     * @returns {*}
     */
    function testPromiseInterval(){
        var getPicture = function(config){
            var dtd = $.Deferred();
            var success = function(event){
                console.log('work finished');
                dtd.notify(event);
                /**
                 * 触发resolve后就不在触发notify
                 * dtd.resolve(event);
                 */
            };
            var error = function(event){
                console.log('work error');
                debugger;
                dtd.reject(event);
            };
            var watchID = setInterval(success,500);
            return dtd.promise();
        };
        var promise = getPicture([]);

        promise.done(function(event){
                console.log('done');
            }).fail(function(event){
                console.log('fail');
            }).done(function(event){
                console.log('another done')
            }).fail(function(event){
                console.log('another fail');
            }).progress(function(event){
                console.log('progress.....');
            });
    }

})(jQuery);
